package com.example.mr5.money.main.userdefined;/**
 * Created by Mr.5
 * Date：2016/9/25.
 * Time：20:21
 */

import android.animation.ObjectAnimator;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.mr5.money.R;
import com.example.mr5.money.model.CommonType;

import java.util.List;

/**
 * Created by Mr.5
 * Date：2016/9/25.
 * Time：20:21
 */
public class UserDefinedListAdapter extends BaseExpandableListAdapter {

    private Context context;
    private List<CommonType> parentList;
    private List<CommonType> childList;
    private boolean isEditable = false;

    public UserDefinedListAdapter(Context context, List<CommonType> parentList, List<CommonType> childList) {
        this.context = context;
        this.parentList = parentList;
        this.childList = childList;

    }

    @Override
    public int getGroupCount() {
        return parentList.size();
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return parentList.get(groupPosition).getChildrenList().size();
    }

    @Override
    public Object getGroup(int groupPosition) {
        return parentList.get(groupPosition);
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return parentList.get(groupPosition).getChildrenList().get(childPosition);
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        parentViewHolder holder;
        if (null == convertView) {
            holder = new parentViewHolder();
            convertView = LayoutInflater.from(context).inflate(R.layout.item_user_defined_list_parent, null);
            holder.parentTitle = (TextView) convertView.findViewById(R.id.tv_parent_title);
            holder.arrow = (ImageView) convertView.findViewById(R.id.img_arrow);
            convertView.setTag(holder);
        } else {
            holder = (parentViewHolder) convertView.getTag();
        }
        holder.parentTitle.setText(parentList.get(groupPosition).getName());
        holder.arrow.clearAnimation();
        ObjectAnimator objectAnimator = new ObjectAnimator().ofFloat(holder.arrow, "rotation", isExpanded ? 180f : 0f).setDuration(300);
        objectAnimator.start();

        return convertView;
    }

    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        childViewHolder holder;
        if (null == convertView) {
            holder = new childViewHolder();
            convertView = LayoutInflater.from(context).inflate(R.layout.item_user_defined_list_child, null);
            holder.childTitle = (TextView) convertView.findViewById(R.id.tv_child_title);
            holder.delete = (ImageView) convertView.findViewById(R.id.img_delete);
            holder.add = (ImageView) convertView.findViewById(R.id.button_add);
            convertView.setTag(holder);
        } else {
            holder = (childViewHolder) convertView.getTag();
        }

        if (parentList.get(groupPosition).getChildrenList().get(childPosition) == null) {
            holder.childTitle.setVisibility(View.GONE);
            holder.delete.setVisibility(View.GONE);
            if (isEditable) {
                holder.add.setVisibility(View.VISIBLE);
            } else {
                holder.add.setVisibility(View.GONE);
            }

        } else {
            holder.add.setVisibility(View.INVISIBLE);
            holder.childTitle.setVisibility(View.VISIBLE);
            holder.childTitle.setText(parentList.get(groupPosition).getChildrenList().get(childPosition).getName());
            if (isEditable) {
                holder.delete.setVisibility(View.VISIBLE);
            } else {
                holder.delete.setVisibility(View.INVISIBLE);
            }
        }
        return convertView;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }

    public void setEditable(boolean editable) {
        this.isEditable = editable;
    }

    public boolean getEditable() {
        return isEditable;
    }

    private class parentViewHolder {
        TextView parentTitle;
        ImageView arrow;
    }

    private class childViewHolder {
        TextView childTitle;
        ImageView delete;
        ImageView add;
    }
}
